
/**
 * get a request token.
 *
 * @param {Object} event
 */
function RequestToken(event){
    var url = "/gaeoauth";
    var ajax = new Ajax.Request(url, {
        parameters: {
            request_token_url: $('request_token_url').value,
            authorize_token_url: $('authorize_token_url').value,
            access_token_url: $('access_token_url').value,
            consumer_key: $('consumer_key').value,
            consumer_secret: $('consumer_secret').value,
            oauth_signature_method: $('oauth_signature_method').value,
            http_method: $('http_method').value,
            oauth_cmd: "RequestToken"
        },
        method: 'get',
        onSuccess: function(transport){
            var response = new String(transport.responseText);
            
            updateConsole(response);
            
            if (response.indexOf("OAuthException") == -1) {
                var authorize_index_start = response.indexOf("<BeginAuthorize>");
                var authorize_index_end = response.indexOf("<EndAuthorize>");
                
                if (authorize_index_start != 1 && authorize_index_end != -1) {
                    var authorize_link = response.substring(authorize_index_start + "<BeginAuthorize>".length, authorize_index_end);
                    
                    $('authorize_token_button').disabled = "";
                    Event.observe('authorize_token_button', 'click', function(){
                        window.open(authorize_link, 'authorize_window_name');
                        $('access_token_button').disabled = "";
                        Event.observe('access_token_button', 'click', AccessToken.bind(this));
                    });
                }
                else {
                    $('response_console').value = response.substring(response.indexOf("OAuthException"));
                }
            }
        },
        onFailure: function(){
            alert('Something went wrong...')
        }
    });
}

function AccessToken(event){
    var url = "/gaeoauth";
    var ajax = new Ajax.Request(url, {
        parameters: {
			consumer_key: $('consumer_key').value,
            oauth_cmd: "AccessToken"
        },
        method: 'get',
        onSuccess: function(transport){
            var response = new String(transport.responseText);
            
            updateConsole(response);
            
            if (response.indexOf("OAuthException") == -1) {
                var access_index_start = response.indexOf("<BeginAccessToken>");
                var access_index_end = response.indexOf("<EndAccessToken>");
                
                if (access_index_start != 1 && access_index_end != -1) {
                    var access_token = response.substring(access_index_start + "<BeginAccessToken>".length, access_index_end);
                    var split_index = access_token.indexOf(";");
                    if (split_index != -1) {
                        var oauth_token = access_token.substring(0, split_index);
                        var oauth_token_secret = access_token.substring(split_index + 1);
                        $('access_token').value = oauth_token.substring("oauth_token=".length);
                        $('access_token_secret').value = oauth_token_secret.substring("oauth_token_secret=".length);
                        
                        $('run_api_button').disabled = "";
                        Event.observe('run_api_button', 'click', RunAPI.bind(this));
                    }
                }
            }
            else {
                $('response_console').value = response.substring(response.indexOf("OAuthException"));
            }
        },
        onFailure: function(){
            alert('Something went wrong...')
        }
    });
    
}

function RunAPI(event){
    var url = "/gaeoauth";
    var ajax = new Ajax.Request(url, {
        parameters: {
			consumer_key: $('consumer_key').value,
            api_url: $('api_url').value,
            oauth_cmd: "RunAPI"
        },
        method: 'get',
        onSuccess: function(transport){
            var response = new String(transport.responseText);
            
            updateConsole(response);
            
            if (response.indexOf("OAuthException") == -1) {
                //...
            }
            else {
                $('response_console').value = response.substring(response.indexOf("OAuthException"));
            }
        },
        onFailure: function(){
            alert('Something went wrong...')
        }
    });
}

/**
 * update request_console and response_console.
 *
 * @param {Object} response
 */
function updateConsole(response){
    var request_index_start = response.indexOf("<BeginRequest>");
    var request_index_end = response.indexOf("<EndRequest>");
    
    if (request_index_start != -1 && request_index_end != -1) {
        $('request_console').value = response.substring(request_index_start, request_index_end + "<EndRequest>".length);
        findRequestParam(response);
    }
    
    var response_index_start = response.indexOf("<BeginResponse>");
    var response_index_end = response.indexOf("<EndResponse>");
    
    if (response_index_start != -1 && response_index_end != -1) {
        $('response_console').value = response.substring(response_index_start, response_index_end + "<EndResponse>".length);
        getOauthTokenInfo($('response_console').value);
    }
}

/**
 *  find request param
 * @param {Object} response
 */
function findRequestParam(response){
    $('oauth_timestamp').value = findParamValue(response, 'oauth_timestamp');
    $('oauth_nonce').value = findParamValue(response, 'oauth_nonce');
    $('oauth_signature').value = findParamValue(response, 'oauth_signature');
}

/**
 * get oauth token and oauth token secret.
 * @param {Object} response
 */
function getOauthTokenInfo(response){
    var index_start = response.indexOf("oauth_token");
    if (index_start == -1) {
        return;
    }
    var index_end = response.indexOf('&', index_start);
    if (index_end == -1) {
        return;
    }
    $('access_token').value = response.substring(index_start + "oauth_token=".length, index_end);
    
    index_start = response.indexOf("oauth_token_secret");
    if (index_start == -1) {
        return;
    }
    index_end = response.indexOf('&', index_start);
    if (index_end == -1) {
        return;
    }
    $('access_token_secret').value = response.substring(index_start + "oauth_token_secret=".length, index_end);
}

/**
 * find Param Value in request body.
 * @param {Object} response
 * @param {Object} key
 */
function findParamValue(response, key){
    var start_index = response.indexOf(key);
    if (start_index != -1) {
        var end_index = response.indexOf(',', start_index);
        if (end_index != -1) {
            return response.substring(start_index + key.length + 1 + 1, end_index - 1);
        }
        else {
            return response.substring(start_index + key.length + 1 + 1, response.length - 1);
        }
    }
    else {
        return "";
    }
}

/**
 * init value for t.qq
 *
 */
function initValueForQQ(){
    $('request_token_url').value = "https://open.t.qq.com/cgi-bin/request_token";
    $('authorize_token_url').value = "https://open.t.qq.com/cgi-bin/authorize";
    $('access_token_url').value = "https://open.t.qq.com/cgi-bin/access_token";
	$('consumer_key').value = "28d65142a35d488585929fe1e92b44c2";
    $('api_url').value = "http://open.t.qq.com/api/user/info";
}

Event.observe(window, 'load', function(){
    initValueForQQ();
	
    Event.observe('request_token_button', 'click', RequestToken.bind(this));
});
